<?php
namespace app\dboard\controller;
use think\captcha;
use think\facade\Cookie;
use think\facade\Session;
use think\facade\Url;
use think\Db;
class Courseware extends Base
{
    public function initialize() {
        parent::initialize();
        $returnUrl=Url::build('dboard/mlogin/sign');
        if(!isset($this->AdminAccount['AdminID']) || intval($this->AdminAccount['AdminID'])<=0 || !isset($this->AdminAccount['sid'])){
            if($this->request->isAjax()){
                return $this->print_result(['Code'=>9999,'Msg'=>'请登录']);
            }
            return $this->redirect($returnUrl);
        }
    }
    public function courseware_lists(){
        return view('courseware/lists');
    }
    public function ajaxList(){
        if (!$this->request->isAjax()) {
            $this->res_data['Msg'] = '请求方式错误';
            return $this->print_result($this->res_data);
        }
        $input = $this->request->param(false);
        $page_limit      = isset($input['length'])?$input['length']:10;
        $page_start      = isset($input['start'])?$input['start']:0;
        $p = 1;
        if ($page_start > 0) {
            $p = ceil($page_start / ($page_limit - 1));
        }
        $sid = isset($input['sid'])?$input['sid']:0;
        if (!$sid) {
          $this->res_data['Msg'] = "缺少必填信息";
          return $this->res_data;
        }
        $store= Db::name('store')->where([['id','=',$sid]])->find();
        if (!$store) {
          $this->res_data['Msg'] = "无此门店";
          return $this->res_data;
        }
        $where = [];
        $where[] = ['ca.sid','=',$sid];
        $where[] = ['ca.type','=',1];
        $where[] = ['ca.status','=',1];
        $where[] = ['c.status','=',1];
        $where[] = ['ca.start_time','<',time()];
        $where[] = ['ca.end_time','>=',time()];
        $where[] = ['c.file','like','%ppt%'];
        $field = '*';
        $count=Db::name('courseware_authorize')->alias('ca')->leftJoin('courseware c','ca.course_id=c.course_id')->where($where)->count();
        $list  = Db::name('courseware_authorize')->alias('ca')
              ->leftJoin('courseware c','ca.course_id=c.course_id')
              ->field('c.*')
              ->where($where)
              ->order('c.id ASC')
              ->limit($page_start, $page_limit)->select();
        foreach($list as &$v){
            $v['create_date'] = date('Y-m-d H:i',$v['create_time']);
            $file = json_decode($v['file'],1);
            $v['content'] = '';
            if (count($file)) {
                foreach ($file as $key => $value) {
                    if (isset($value[2]))
                        $v['content'] .= $value[2].',';
                }
                $v['content'] = trim($v['content'],',');
            }
        }
        
        $this->res_data['Msg']='OK';
        $this->res_data['Code']=0;
        $this->res_data['Data']=['courseware'=>$list,'Data'=>$list,'recordsTotal'=>$count];
        $this->res_data['recordsTotal']=$count;
        return $this->print_result($this->res_data);
    }
    public function edit_courseware(){
        $input = $this->request->param(false);
        $id = isset($input['id'])?$input['id']:0;
        $courseware = [];
        if (!$id) {
            return $this->print_result(['Code'=>1,'Msg'=>'缺少参数']);
        }
        $courseware = Db::name('courseware')->where(['id'=>$id])->find();
        if (isset($courseware['file']) && $courseware['file']) {
            $courseware['file'] = json_decode($courseware['file'],1);
            if (is_array($courseware['file']) && count($courseware['file'])) {
                foreach ($courseware['file'] as $key => &$value) {
                    $multi_file = [];
                    $ex = explode(',', trim($value[1]));
                    if (count($ex)) {
                        foreach ($ex as $k => &$mf) {
                            if (explode('.', $mf)[1]!='ppt' && explode('.', $mf)[1]!='pptx') continue;
                            $mf_name = Db::name('file')->where(['path'=>$mf])->value('name');
                            if (!$mf_name) {
                                if ($value[2]=='视频') {
                                    $mf_name = '视频';
                                } else {
                                    $mf_name = '文件';
                                }
                            }
                            $mf = config('upload_domain').$mf;
                            $multi_file[$k] = ['mf_name'=>$mf_name,'mf'=>$mf];
                        }
                    }
                    $value[3] = $multi_file;
                }
            }
        }
        $courseware_type = Db::name('courseware_type')->where(['status'=>1])->select();
        $courseware_course = Db::name('courseware_course')->where(['status'=>1,'pid'=>0])->select();
        $pid3 = $courseware['course_id'];
        $pid2 = Db::name('courseware_course')->where(['id'=>$pid3])->value('pid');
        $pid1 = Db::name('courseware_course')->where(['id'=>$pid2])->value('pid');
        $this->assign('courseware_type',$courseware_type);
        $this->assign('courseware_course',$courseware_course);
        $this->assign('courseware',$courseware);
        $this->assign('pid1',$pid1);
        $this->assign('pid2',$pid2);
        $this->assign('pid3',$pid3);
        return view('courseware/edit_courseware');
    }
    public function courseware_info(){
        $input = $this->request->param(false);
        $id = isset($input['id'])?$input['id']:0;
        $courseware = [];
        if (!$id) {
            return $this->print_result(['Code'=>1,'Msg'=>'缺少参数']);
        }
        $courseware = Db::name('courseware')->where(['id'=>$id])->find();
        $courseware['has_video'] = 0;
        $courseware['has_pdf'] = 0;
        $courseware['video'] = [];
        $courseware['pdf'] = [];
        if (isset($courseware['file']) && $courseware['file']) {
            $courseware['file'] = json_decode($courseware['file'],1);
            if (is_array($courseware['file']) && count($courseware['file'])) {
                foreach ($courseware['file'] as $key => &$value) {
                    $multi_file = [];
                    $ex = explode(',', trim($value[1]));
                    if (count($ex)) {
                        foreach ($ex as $k => &$mf) {
                            if (explode('.', $mf)[1]!='ppt' && explode('.', $mf)[1]!='pptx') continue;
                            $mf_name = Db::name('file')->where(['path'=>$mf])->value('name');
                            if (!$mf_name) {
                                if ($value[2]=='视频') {
                                    $mf_name = '视频';
                                } else {
                                    $mf_name = '文件';
                                }
                            }
                            $mf = config('upload_domain').$mf;
                            $multi_file[$k] = ['mf_name'=>$mf_name,'mf'=>$mf];
                        }
                    }
                    $value[3] = $multi_file;
                    if ($value[2]=='视频') {
                        $courseware['has_video'] = 1;
                        $courseware['video'] = array_merge($courseware['video'],$multi_file);
                    } else {
                        $courseware['has_pdf'] = 1;
                        $courseware['pdf'] = array_merge($courseware['pdf'],$multi_file);
                    }
                }
            }
        }
        $pid3 = $courseware['course_id'];
        $courseware_course_name3 = $courseware['course'];
        $courseware_course2 = Db::name('courseware_course')->where(['id'=>$pid3])->field('pid,name')->find();
        $pid2 = $courseware_course2['pid'];
        $courseware_course_name2 = $courseware_course2['name'];
        $courseware_course1 = Db::name('courseware_course')->where(['id'=>$pid2])->field('pid,name')->find();
        $pid1 = $courseware_course2['pid'];
        $courseware_course_name1 = $courseware_course1['name'];
        $this->assign('courseware',$courseware);
        $this->assign('pid1',$pid1);
        $this->assign('pid2',$pid2);
        $this->assign('pid3',$pid3);
        $this->assign('courseware_course_name1',$courseware_course_name1);
        $this->assign('courseware_course_name2',$courseware_course_name2);
        $this->assign('courseware_course_name3',$courseware_course_name3);
        return view('acourseware/courseware_info');
    }
    public function authorize(){
        if ($this->request->isPost()) {
            // $param = $this->request->param();
            // $id = isset($param['id'])?intval($param['id']):0;
            // $sid = isset($param['sid'])?intval($param['sid']):0;
            // $courseware = Db::name('courseware')->where('id',$id)->find();
            // $teacher = Db::name('teacher')->where(['status'=>1,'sid'=>$sid])->select();
            // // 已授权的店
            // $courseware_authorize = Db::name('courseware_authorize')->where(['courseware_id'=>$id,'status'=>1,'type'=>2])->select();
            // return $this->print_result(['Code'=>0,'Msg'=>'成功','Data'=>['courseware'=>$courseware,'store'=>$teacher,'courseware_authorize'=>$courseware_authorize]]);
        }
        $input = $this->request->param(false);
        $id = isset($input['id'])?$input['id']:0;
        $sid = isset($this->AdminAccount['sid'])?$this->AdminAccount['sid']:0;
        $courseware = [];
        if (!$id || !$sid) {
            return $this->print_result(['Code'=>1,'Msg'=>'缺少参数']);
        }
        $courseware = Db::name('courseware')->where('id',$id)->find();
        $teacher = Db::name('teacher')->where(['status'=>1,'sid'=>$sid])->select();
        if ($teacher) {
            foreach ($teacher as $key => &$value) {
                $value['avatar'] = config('upload_domain').$value['avatar'];
            }
        }
        // 已授权的店
        $courseware_authorize = Db::name('courseware_authorize')->where(['courseware_id'=>$id,'status'=>1,'type'=>2])->value('group_concat(teacher_id) as teacher_ids');
        $this->assign('id',$id);
        $this->assign('courseware',$courseware);
        $this->assign('store',$teacher);
        $this->assign('courseware_authorize',$courseware_authorize);
        return view('courseware/authorize');
    }
    public function authorize_list(){
        return view('courseware/authorize_list');
    }
    public function authorizelist()
    {
        $tid = $this->request->param('tid');
        $start_time = '';
        $end_time = '';
        $courseware_authorize = Db::name('courseware_authorize')->where(['teacher_id'=>$tid,'type'=>2,'status'=>1])->find();
        if ($courseware_authorize) {
            $start_time = date('Y-m-d H:i',$courseware_authorize['start_time']);
            $end_time = date('Y-m-d H:i',$courseware_authorize['end_time']);
        }
        $this->assign('start_time',$start_time);
        $this->assign('end_time',$end_time);
        $this->assign('tid',$tid);
        return view('courseware/authorizelist');
    }
    public function editCoursewareauthorize()
    {
        if (!$this->request->isAjax()) {
            $this->res_data['Msg'] = '请求方式错误';
            return $this->print_result($this->res_data);
        }
        $param    = $this->request->param(false);
        // var_dump($param);exit;
        $courseware= Controller('Courseware', 'logic');
        $result            = $courseware->updateCoursewareauthorize($param);
        return $this->print_result($result);
    }
}
